package com.star.mall.persistence.service.impl;

import com.star.mall.base.model.Lock;
import com.star.mall.persistence.entity.UserScore;
import com.star.mall.persistence.mapper.UserScoreMapper;
import com.star.mall.persistence.service.IUserScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.star.mall.utils.ContextUtil;
import io.swagger.models.auth.In;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户积分表 服务实现类
 * </p>
 *
 * @author StarQ
 * @since 2021-08-02
 */
@Service
public class UserScoreServiceImpl extends ServiceImpl<UserScoreMapper, UserScore> implements IUserScoreService {

    @Resource
    RedisTemplate<String, Integer> redisTemplate;

    @Override
    public void initScore(int score, String userId) {
        UserScore userScore = new UserScore();
        userScore.setUserId(userId);
        userScore.setScoreSrc(1);
        userScore.setScore(score);
        userScore.setSrcRemarks("初始积分");
        userScore.setType(UserScore.INCOME);

        save(userScore);
    }

    @Override
    @Lock(key = "#userId")
    public void addScore(int score, String userId) {
        Integer count = redisTemplate.opsForValue().get("count");
        if (count==null)
            count = 0;
        count+=score;
        redisTemplate.opsForValue().set("count", count);
    }
}
